package com.lz.ad.canal.common;

import com.lz.common.util.GetCfg;
import com.lz.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Li Tiefei on 2018/11/26.
 */
@Slf4j
public class Config {
    /** zk配置节点路径 **/
    public static final String ZK_CONFIG_PATH = GetCfg.getString("Zk_Config_path", "/config/lzmh_ad_canal");
    /** zk服务注册节点路径 **/
    public static final String ZK_SERVER_PATH = GetCfg.getString("Zk_Server_path", "/server/lzmh_ad_canal/lzmh_ad_canal");
    /** canal ip **/
    public static String CANAL_IP;
    /** canal ip **/
    public static Integer CANAL_PORT;

    /** RabbitMq Route模式下路由Key的键值对 */
    public static Map<String, String> RABBIT_LZMH_ROUTING_KEY_MAP = new HashMap<>();

    /**
     * 配置初始化
     */
    public static void init(){
        CANAL_IP = GetCfg.getString("canal_ip", "192.168.10.7");
        CANAL_PORT = GetCfg.getInt("canal_port", 11111);

        RABBIT_LZMH_ROUTING_KEY_MAP = new HashMap<>();
        setRoutingKey(GetCfg.getString("rabbit.lzmhad.table.routingkeys", ""));

        log.debug("RABBIT_LZMH_ROUTING_KEY_MAP = {}", RABBIT_LZMH_ROUTING_KEY_MAP);
    }

    /**
     * 初始化RabbitMq的 Routing Key
     */
    private static void setRoutingKey(String tableRoutekeys) {
        if (StringUtils.isNotEmpty(tableRoutekeys)) {
            String[] rabbitTableRoutekeysArr = tableRoutekeys.split(",");
            for (String tableKey : rabbitTableRoutekeysArr) {
                if (StringUtils.isNotEmpty(tableKey)) {
                    RABBIT_LZMH_ROUTING_KEY_MAP.put(tableKey.trim(), tableKey.trim());
                }
            }
        }
    }
}
